package lormi.meipin.service.impl;


import lormi.meipin.common.Constants;
import lormi.meipin.common.LMRongYunAPI;
import lormi.meipin.common.SQLJoint;
import lormi.meipin.dto.*;
import lormi.meipin.entity.*;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.ITalentService;
import lormi.meipin.service.ITokenService;
import lormi.meipin.util.CacheUtil;
import lormi.meipin.util.StringUtil;
import lormi.meipin.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TalentService extends BaseService implements ITalentService{

	@Autowired
	private ITokenService tokenService;

	@Override
	@Transactional
	public LoginVo regTalent(RegTalent regTalent) throws LMException {
		//查询该手机是否已注册
		if (memberMapper.selectByPhone(regTalent.getPhone())!=null) {
			throw new LMException(100021);
		}
		//如果没有注册则注册该手机号
		if (memberMapper.insert(new Member(regTalent.getPhone(),new Byte("1")))<1) {
			throw new LMException(150002);
		}
		Member member = memberMapper.selectByPhone(regTalent.getPhone());
		//插入用户信息数据
		talentMapper.insert(new Talent(member.getId(),regTalent));
		if (talentMapper.selectByMemId(member.getId())==null) {
			throw new LMException(150008);
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				//插入用户自己的邀请
				invitationCodeMapper.insert(new InvitationCode(member.getId(),StringUtil.get6Code()));
				//插入相应的日志
                locationMapper.insert(new Location(regTalent,member.getId(),member.getUsertype()));
				loginLogMapper.insert(new LoginLog(regTalent,member.getId()));
				locationLogMapper.insert(new LocationLog(regTalent.getLongitude(),regTalent.getLatitude(),member.getId()));
				//插入邀请日志
				String code = CacheUtil.getCacheUtil().getOtherCacheToStr("invite_"+regTalent.getPhone());
				if (StringUtil.isNotNull(code)) {//存在邀请码
					InvitationCode invitationCode = invitationCodeMapper.selectByCode(Integer.valueOf(code));
					invitationLogMapper.insert(new InvitationLog(invitationCode.getCode(),invitationCode.getUserid(),member.getId(),invitationCode.getCreatetime(),member.getCreatetime()));
				}
			}
		}).start();

		//集成返回结果
		LoginVo loginVo = new LoginVo();
		loginVo.setId(member.getId());
		loginVo.setUsertype(member.getUsertype());
		loginVo.setMobile(member.getMobile());
		loginVo.setCreatetime(new Date());
		loginVo.setUpdatetime(loginVo.getCreatetime());
		loginVo.setToken(tokenService.insertToken(member.getId()).getToken());
		return loginVo;
	}

	@Override
	public List<TalentSearchVo> searchTalent(TalentSearchDto talentSearchDto) throws LMException {
		
		String sql = SQLJoint.getSearchTalentSql(talentSearchDto);
		int offset = (talentSearchDto.getPagenow()-1)*talentSearchDto.getPagesize();
		sql += " order by e.updatetime desc limit "+offset+", "+talentSearchDto.getPagesize()+";";
		
		List<TalentSearchVo> list = talentMapper.selectSearchVo(sql);
		if (list==null || list.size()<1) {
			throw new LMException(100028);
		}
		return list;
	}

	@Override
	public List<TalentSearchVo> nearByTalent(TalentNearByDto talentNearByDto) throws LMException {
		int offset = (talentNearByDto.getPagenow()-1)*talentNearByDto.getPagesize();
		List<TalentSearchVo> list = talentMapper.selectNearbyVo(talentNearByDto.getLongitude(), talentNearByDto.getLatitude(), 
				offset,talentNearByDto.getPagesize());

		if (list==null || list.size()<1) {
			throw new LMException(100028);
		}
		return list;
	}

	@Override
	public Talent getTalent(Integer userid) throws LMException {
		Talent talent = talentMapper.selectByMemId(userid);
		talent.setSexname(systemInfoMapper.selectByKey("sex_"+talent.getSex()).getSvalue());
		talent.setWorkingstatename(systemInfoMapper.selectByKey("work_"+talent.getWorkingstate()).getSvalue());
		talent.setLabels(labelTemplateMapper.selectByList(StringUtil.getSplit(talent.getLabel(), ";")));
		return talent;
	}

	@Override
	public void editTalent(TalentEditDto talentEditDto) throws LMException {
		if (talentMapper.update(new Talent(talentEditDto))<1) {
			throw new LMException(100036);
		}
	}

	@Override
	public TalentCenterVo getTalentCenter(Integer userid) throws LMException {
		TalentCenterVo talentCenterVo = talentMapper.selectTalentCenter(userid);
		talentCenterVo.setWorkstatename(systemInfoMapper.selectByKey("work_"+talentCenterVo.getWorkingstate()).getSvalue());
		talentCenterVo.setIntegrity(getIntegrity(talentCenterVo));
		return talentCenterVo;
	}

	@Override
	public TalentSeeVo seeTalent(Integer userid, Integer talentid, String sharecode) throws LMException {
		//1. 判断是否是分享请求
		if (sharecode!=null && "SHLF8DF8ASD8".equals(sharecode)) {
			//分享请求没有限制
		}else {
			//如果是商家请求，判断是否已发布职位
			if (positionMapper.selectCount(userid)<1) {//没有发布职位的返回异常
				throw new LMException(100020);
			}
			//判断商家是否查看过个人，没有查看过的推送查看通知
			FocusTalent focusTalent = focusTalentMapper.selectByMeridAndMemid(userid, talentid, 2);//2:表示是查看的类型
			if (focusTalent==null) {
				MerChant merChant = merChantMapper.selectByMerId(userid);
				LMRongYunAPI.push(String.valueOf(talentid),merChant.getName(), 21);//21:表示商家查看推送
				//插入查看通知纪录
				focusTalentMapper.insert(new FocusTalent(merChant.getMerid(),talentid,2));
			}
		}
		TalentSeeVo talentSeeVo = talentMapper.selectTalentSee(talentid);
		talentSeeVo.setLabel(Constants.getLabel(labelTemplateMapper.selectByList(StringUtil.getSplit(talentSeeVo.getLabels(),";"))));
		talentSeeVo.setWorkeds(workedMapper.selectList(talentid));
		talentSeeVo.setTraineds(trainedMapper.selectByMemId(talentid));
		talentSeeVo.setIsfocus(focusTalentMapper.selectByMeridAndMemid(userid, talentid, 1)!=null);
		talentSeeVo.setSexname(systemInfoMapper.selectByKey("sex_"+talentSeeVo.getSex()).getSvalue());
		talentSeeVo.setWorkingstatename(systemInfoMapper.selectByKey("work_"+talentSeeVo.getSex()).getSvalue());
		return talentSeeVo;
	}

	@Override
	public List<TalentNoticeVo> noticeTalent(CurrencyDto currencyDto) throws LMException {
		List<TalentNoticeVo> list = focusTalentMapper.selectTalentNotice(currencyDto.getUserid(),currencyDto.getOffset(), currencyDto.getPagesize());
		if (list==null || list.size()<1) {
			throw new LMException(100028);
		}
		return list;
	}

	public String getIntegrity(TalentCenterVo t){
		int times = 0;
		if(t!=null){
			if(t.getName()!=null){
				times+=1;
			}
			if(t.getPositionclass()!=null){
				times+=1;
			}
			if(t.getWorkingstate()!=null){
				times+=1;
			}
			if(t.getIsExpectWork()>0){
				times+=1;
			}
			if(t.getIsTrained()>0){
				times+=1;
			}
			if(t.getIsWorked()>0){
				times+=1;
			}
			if(t.getFocusTimes()>0){
				times+=2;
			}
		}
		return String.valueOf(times*10);
	}

	
}
